﻿using System;
using System.Linq;
using System.Text;
using PersonalAccoutingProgram.Data;
using PersonalAccoutingProgram.Logic;

class ExpensesReferenceMaker : ReferenceMaker 
{
    public static decimal MakeAverageReferenceDay(DateTime date, string username, string category)
    {
        decimal averageAmount = 0;       
        if (category == String.Empty)
        {
            var allExpensesForDate =
            ExpensesViewDataClassesDataContext.GetAllExpensesForDay(
                date, username);
            averageAmount = CalculateAverageAmount(allExpensesForDate);          
        }
        else
        { 
            var allExpensesForDate =
            ExpensesViewDataClassesDataContext.GetExpensesForDayByCategory(
                date, username, category);
            averageAmount = CalculateAverageAmount(allExpensesForDate);  
        }

        return averageAmount;       
    }

    

    public static decimal MakeTotalReferenceDay(DateTime date, string username, string category)
    {
        decimal expensesSum = 0;        

        if (category == String.Empty)
        {
            var allExpensesForDate = 
            ExpensesViewDataClassesDataContext.GetAllExpensesForDay(
                date, username);
            expensesSum = CalculateSum(allExpensesForDate);
        }
        else
        {
            var allExpensesForDate =
            ExpensesViewDataClassesDataContext.GetExpensesForDayByCategory(
                date, username, category);
            expensesSum = CalculateSum(allExpensesForDate);
        }

        return expensesSum;
    }

    

    public static decimal MakeTotalReferenceMonth(string date, string username, string category)
    { 
        decimal expensesSum = 0;
        DateTime dateForCheck = DateTime.Parse(date);        
        if (category == String.Empty)
        {
            var allExpensesForUser = 
            ExpensesViewDataClassesDataContext.GetExpensesForMonth(
                username, dateForCheck);
            expensesSum = CalculateSum(allExpensesForUser);           
        }
        else
        {
            var allExpensesForUser =
            ExpensesViewDataClassesDataContext.GetExpensesForMonthByCategory(
                username, dateForCheck, category);
            expensesSum = CalculateSum(allExpensesForUser);
        }
   
        return expensesSum;
    }

    public static decimal MakeTotalReferenceYear(string date, string username, string category)
    {
        decimal expensesSum = 0;
        DateTime dateForCheck = DateTime.Parse(date);    

        if (category == String.Empty)
        {
            var allExpensesForUser =
            ExpensesViewDataClassesDataContext.GetExpensesForYear(
                username, dateForCheck);
            expensesSum = CalculateSum(allExpensesForUser);
        }
        else
        {
            var allExpensesForUser =
            ExpensesViewDataClassesDataContext.GetExpensesForYearByCategory(
                username, category, dateForCheck);
            expensesSum = CalculateSum(allExpensesForUser);
        }

        return expensesSum;
    }

    public static string GetAllAmountsForCurrentUser(string username, string category)
    {         
        string expensesStr = String.Empty;
        if (category == String.Empty)
        {
            var allExpensesForUser =
            ExpensesViewDataClassesDataContext.GetAllUserExpenses(username);
            expensesStr = GetAllExpensesString(allExpensesForUser);
            return expensesStr;
        }
        else
        {
            var allExpensesForUser =
            ExpensesViewDataClassesDataContext.GetAllUserExpensesByCategory(
                username, category);
            expensesStr = GetAllExpensesString(allExpensesForUser);
            return expensesStr;
        }
    }

    private static string GetAllExpensesString(IQueryable<ExpensesView> allExpensesForUser)
    {
        if (allExpensesForUser.Count(exp => exp.ExpenseAmount != 0.00M) > 0)
        {
            string amountStr = MakeAllExpensesString(allExpensesForUser);
            return amountStr.ToString();
        }
        else
        {
            return String.Empty;
        }
    }

    private static string MakeAllExpensesString(IQueryable<ExpensesView> allExpensesForUser)
    {
        StringBuilder amountStr = new StringBuilder();
        foreach (var currentExpenses in allExpensesForUser)
        {
            string date = currentExpenses.ExpenseDate.ToString(DateFormat.DATE_TIME_FORMAT);
            string categoryStr = currentExpenses.CategoryName;
            decimal amount = currentExpenses.ExpenseAmount;
            amountStr.AppendFormat("{0} {1,2} {2:C}", date, categoryStr, amount);
            amountStr.AppendLine();
        }
        return amountStr.ToString();
    }
}